package com.smbms.controller;

import com.smbms.pojo.Bill;
import com.smbms.pojo.User;
import com.smbms.service.BillService;
import com.smbms.util.PageParams;
import com.smbms.util.PageResult;
import com.smbms.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 类名：BillController
 * 作者：小涛
 * 创建时间：2021/8/25-20:16
 * 功能：
 */
@Controller
@RequestMapping("/bill")
public class BillController {
    @Resource
    private BillService billService;
    Result result;

    @ResponseBody
    @RequestMapping("/findPage")
    public Result findPage(PageParams pageParams) {
        //查询数量，列表
        PageResult<Bill> pageResult = billService.findPage(pageParams);
        result = new Result(true,"订单查询成功",pageResult);
        return result;
    }

    @ResponseBody
    @RequestMapping("/delBill")
    public Result delUser(Integer id) {
        int count = billService.delBill(id);
        if(count > 0){
            result = new Result(true, "删除用户成功");
        }else {
            result = new Result(false, "删除用户失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/checkBillCodeAdd")
    private Result checkBillCodeAdd(Bill bill) {
        int count = billService.checkBillCode(bill);
        if (count > 0) {
            result = new Result(false, "该订单编号已经被使用");
        }else {
            result = new Result(true, "该订单编号可以使用");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/addBill")
    private Result addBill(Bill bill, HttpSession session) {
        //创建人，当前登录的用户
        User loginUser = (User)session.getAttribute("LOGIN_USER");
        bill.setCreatedBy(loginUser.getId());

        int count = billService.addBill(bill);
        if (count>0){
            return new Result(true,"添加成功");
        }else {
            return new Result(false,"添加失败");
        }
    }

    @ResponseBody
    @RequestMapping("/findById")
    private Result findById(Integer id) {
        Bill bill = billService.findById(id);
        return new Result(true,"用户查询成功",bill);
    }

    @ResponseBody
    @RequestMapping("/checkBillCodeUpd")
    private Result checkBillCodeUpd(String billCode, String self) {
        int count = billService.checkBillCodeUpd(billCode,self);
        if(count > 0){
            return new Result(false, "订单编码已被占用");
        }
        return new Result(true, "订单编码可以使用");
    }

    @ResponseBody
    @RequestMapping("/updBill")
    private Result updBill(Bill bill, HttpSession session) throws SQLException, IOException, ClassNotFoundException {
        //修改人
        User loginUser = (User) session.getAttribute("LOGIN_USER");
        bill.setModifyBy(loginUser.getId());

        //修改订单
        int count = billService.updBill(bill);
        if(count > 0){
            return new Result(true, "修改订单成功");
        }
        return new Result(false, "修改订单失败");

    }
}
